import { createRouter, createWebHistory } from 'vue-router'
import { useAuth } from '../composables/useAuth.js'

const routes = [
  {
    path: '/',
    redirect: '/pmpu'
  },
  {
    path: '/pmpu',
    name: 'Home',
    component: () => import('../page/front/pmpu/HomePage.vue')
  },
  {
    path: '/pmpu/test',
    name: 'PersonalityTest',
    component: () => import('../page/front/pmpu/PersonalityTest.vue')
  },
  {
    path: '/pmpu/results/:groupId',
    name: 'TestResults',
    component: () => import('../page/front/pmpu/TestResults.vue')
  },
  {
    path: '/pmpu/contact',
    name: 'Contact',
    component: () => import('../page/front/ContactPage.vue')
  },
  // pnpm1
  {
    path: '/pmpu-scale',
    name: 'Pmpu1Home',
    component: () => import('../page/front/pmpu1/HomePage.vue')
  },
  {
    path: '/pmpu-scale/test',
    name: 'Pmpu1PersonalityTest',
    component: () => import('../page/front/pmpu1/PersonalityTest.vue')
  },
  {
    path: '/pmpu-scale/results/:groupId',
    name: 'Pmpu1TestResults',
    component: () => import('../page/front/pmpu1/TestResults.vue')
  },
  {
    path: '/pmpu-scale/contact',
    name: 'Pmpu1Contact',
    component: () => import('../page/front/ContactPage.vue')
  },

  {
    path: '/five',
    name: 'FiveHome',
    component: () => import('../page/front/five/HomePage.vue')
  },
  {
    path: '/five/test',
    name: 'FivePersonalityTest',
    component: () => import('../page/front/five/PersonalityTest.vue')
  },
  {
    path: '/five/final-results/:groupId',
    name: 'FiveFinalResults',
    component: () => import('../page/front/five/FinalResults.vue')
  },
  {
    path: '/five/contact',
    name: 'FiveContact',
    component: () => import('../page/front/ContactPage.vue')
  },
  // 后台管理路由 - PMPU
  {
    path: '/backstage/pmpu',
    name: 'PmpuBackstage',
    component: () => import('../page/backstage/BackstageLayout.vue'),
    redirect: '/backstage/pmpu/questionnaire',
    children: [
      {
        path: 'questionnaire',
        name: 'PmpuQuestionnaireManagement',
        component: () => import('../page/backstage/QuestionPage.vue')
      },
      {
        path: 'user',
        name: 'PmpuUserManagement',
        component: () => import('../page/backstage/UserPage.vue')
      },
      {
        path: 'answer-record',
        name: 'PmpuAnswerRecord',
        component: () => import('../page/backstage/AnswerRecord.vue')
      }
    ]
  },
  // 后台管理路由 - Five
  {
    path: '/backstage/five',
    name: 'FiveBackstage',
    component: () => import('../page/backstage/BackstageLayout.vue'),
    redirect: '/backstage/five/questionnaire',
    children: [
      {
        path: 'questionnaire',
        name: 'FiveQuestionnaireManagement',
        component: () => import('../page/backstage/QuestionPage.vue')
      },
      {
        path: 'user',
        name: 'FiveUserManagement',
        component: () => import('../page/backstage/UserPage.vue')
      },
      {
        path: 'answer-record',
        name: 'FiveAnswerRecord',
        component: () => import('../page/backstage/AnswerRecord.vue')
      }
    ]
  },
   // 后台管理路由 - PMPU-scale
  {
    path: '/backstage/pmpu-scale',
    name: 'Pmpu1Backstage',
    component: () => import('../page/backstage/BackstageLayout.vue'),
    redirect: '/backstage/pmpu/questionnaire',
    children: [
      {
        path: 'questionnaire',
        name: 'Pmpu1QuestionnaireManagement',
        component: () => import('../page/backstage/QuestionPage.vue')
      },
      {
        path: 'user',
        name: 'Pmpu1UserManagement',
        component: () => import('../page/backstage/UserPage.vue')
      },
      {
        path: 'answer-record',
        name: 'Pmpu1AnswerRecord',
        component: () => import('../page/backstage/AnswerRecord.vue')
      }
    ]
  },
  // 默认后台重定向到 PMPU
  {
    path: '/backstage',
    redirect: '/backstage/pmpu'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫 - 检查后台访问权限
router.beforeEach((to, from, next) => {
  // 检查是否访问后台管理页面
  if (to.path.startsWith('/backstage')) {
    const { isLoggedIn, isAdmin } = useAuth()
    
    // 检查是否已登录
    if (!isLoggedIn.value) {
      // 未登录，重定向到首页
      next('/pmpu')
      return
    }
    
    // 检查是否为管理员
    if (!isAdmin.value) {
      // 不是管理员，重定向到首页并显示提示
      next('/pmpu')
      return
    }
  }
  
  // 其他路由正常访问
  next()
})

export default router
